#include "LayerHomeDown.h"
#include "cocostudio/CocoStudio.h"
#include "ui/CocosGUI.h"
#include "deprecated/CCDeprecated.h"
#include "ui/UIHelper.h"
#include "base/CCConsole.h"

#include "logic/LLogicTask.h"
#include "logic/LLogicMap.h"

USING_NS_CC;
using namespace ui;


CLayerHomeDown::CLayerHomeDown(void) : m_ContainerPack(nullptr), m_ContainerBody(nullptr), m_Skill(nullptr)
{
}


CLayerHomeDown::~CLayerHomeDown(void)
{
}

bool CLayerHomeDown::init() {
	// 1. super init first
	if ( !CLayerBase::init() )
	{
		return false;
	}

	m_Down = CSLoader::createNode("MainMenu.csb");
	this->setContentSize(m_Down->getContentSize());

	ui::Button* tLoginB = (ui::Button*)ui::Helper::seekWidgetByTag((cocos2d::ui::Widget*)m_Down, 908);
	tLoginB->addTouchEventListener(this, toucheventselector(CLayerHomeDown::onOpenPack));
	tLoginB = (ui::Button*)ui::Helper::seekWidgetByTag((cocos2d::ui::Widget*)m_Down, 906);
	tLoginB->addTouchEventListener(this, toucheventselector(CLayerHomeDown::onGetTask));
	tLoginB = (ui::Button*)ui::Helper::seekWidgetByTag((cocos2d::ui::Widget*)m_Down, 907);
	tLoginB->addTouchEventListener(this, toucheventselector(CLayerHomeDown::onBattle));
	tLoginB = (ui::Button*)ui::Helper::seekWidgetByTag((cocos2d::ui::Widget*)m_Down, 905);
	tLoginB->addTouchEventListener(this, toucheventselector(CLayerHomeDown::onOpenBody));

	addChild(m_Down);

	return true;
}

/*
void CLayerHomeDown::onEnter() {
	CLayerBase::onEnter();
}
void CLayerHomeDown::onExit() {
	CLayerBase::onExit();
}

void CLayerHomeDown::update(float delta) {
	CLayerBase::update(delta);
}
*/

void CLayerHomeDown::onOpenBody(cocos2d::Ref* pSender, cocos2d::ui::TouchEventType type) {
	switch (type)
	{
	case cocos2d::ui::TOUCH_EVENT_ENDED:
		if (nullptr == m_ContainerBody) {
			m_ContainerBody = m_LayerManager->GetLayer(CLayerManager::LayerIContainerBody);
			m_ContainerBody->setPosition(100,200);
			addChild(m_ContainerBody);
		} else {
			m_ContainerBody->setVisible(true);
		}
		break;
	}
}

void CLayerHomeDown::onOpenPack(cocos2d::Ref* pSender, cocos2d::ui::TouchEventType type) {
	switch (type)
	{
	case cocos2d::ui::TOUCH_EVENT_ENDED:
		if (nullptr == m_ContainerPack) {
			m_ContainerPack = m_LayerManager->GetLayer(CLayerManager::LayerIContainerPack);
			addChild(m_ContainerPack);
		} else {
			m_ContainerPack->setVisible(true);
		}
		break;
	}
}

void CLayerHomeDown::onGetTask(cocos2d::Ref* pSender, cocos2d::ui::TouchEventType type) {
	switch (type)
	{
	case cocos2d::ui::TOUCH_EVENT_ENDED: {
		CLLogicMap tMap;
		tMap.EnterMap(GLSLTMapTask);
		m_SceneManager->setCurrentScene(CSceneManager::SCeneIndexTask);
		} break;
	default:
		break;
	}
}

void CLayerHomeDown::onBattle(cocos2d::Ref* pSender, cocos2d::ui::TouchEventType type) {
	switch (type)
	{
	case cocos2d::ui::TOUCH_EVENT_ENDED: {
		//m_SceneManager->setCurrentScene(CSceneManager::SceneIndexBattle);
		if (nullptr == m_Skill) {
			m_Skill = m_LayerManager->GetLayer(CLayerManager::LayerISkill);
			addChild(m_Skill);
		} else {
			m_Skill->setVisible(true);
		}
		} break;
	default:
		break;
	}
}
